//
//  NotificationsViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/1/4.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "NotificationsViewController.h"
#import "NotificationsTableViewCell.h"
#import "OtherDeviceViewController.h"
#import "UserModel.h"
#import "UserTools.h"
#import "HistoryCalendarViewController.h"
#import "NetworkCommand.h"
#import "TotalDeviceSelectedViewController.h"

#import <AFNetworking.h>
#import <FTPopOverMenu.h>
#import <SVProgressHUD.h>
#import <MJRefresh.h>

@interface NotificationsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *notificationsTableView;
@property (weak, nonatomic) IBOutlet UILabel *noDataLabel;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *menuButton;

@property (strong, nonatomic) NSArray * array;
@property (strong, nonatomic) UserModel * userModel;

@end

@implementation NotificationsViewController

-(UserModel *)userModel{
    
    if (!_userModel) {
        
        _userModel = [[UserModel alloc]init];
        
    }
    
    return _userModel;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.notificationsTableView.tableFooterView = [[UIView alloc]init];
    
    self.notificationsTableView.delegate = self;
    
    self.notificationsTableView.dataSource = self;
    
    self.selectSerialNumber = self.userModel.selectedDeviceSerialNumber;
    
    self.selectDate = [NSDate date];
    
    self.notificationsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getAlertList:self.selectDate];
        
        
    }];
    
    [self.notificationsTableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 右上角列表
- (IBAction)touchMenuButton:(id)sender {
    
    [FTPopOverMenu showFromSenderFrame:CGRectMake(self.view.frame.size.width - 40, 20, 40, 40) withMenu:@[@"Other Device"] doneBlock:^(NSInteger selectedIndex) {
        
//        UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"NotificationsTableViewController" bundle:[NSBundle mainBundle]];
//        
//        OtherDeviceViewController * otherDeviceViewController = [storyBoard instantiateViewControllerWithIdentifier:@"OtherDeviceViewController"];
//        
//        [self.navigationController showViewController:otherDeviceViewController sender:nil];
        
        TotalDeviceSelectedViewController *totalDeviceSelectedViewController = [[TotalDeviceSelectedViewController alloc]init];
        
        [self.navigationController showViewController:totalDeviceSelectedViewController sender:nil];
        
    } dismissBlock:^{
        
    }];
    
}

#pragma mark 下拉刷新列表
-(void)beginTableViewMjHeaderReload{
    
    [self.notificationsTableView.mj_header beginRefreshing];
    
}

#pragma mark 获取警报
-(void)getAlertList:(NSDate *)date{
    
    _array = nil;
    
    self.userModel.deviceHistoryEventDataByTimeArray = nil;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    
    NSString *stringDate = [dateFormatter stringFromDate:date];
    
    [self setTitle:stringDate];
    
    //获取时间轴
    [SVProgressHUD showWithStatus:@"Loading..."];
    
    [NetworkCommand getHistoryEventDataByTime:date serialNumber:self.userModel.selectedDeviceSerialNumber success:^{
        
        self.array = self.userModel.deviceHistoryEventDataByTimeArray;
        
        [self.notificationsTableView reloadData];
        
        [SVProgressHUD dismiss];
        
    } failure:^{
        
        [SVProgressHUD showErrorWithStatus:@"Falied."];
        
        [SVProgressHUD dismissWithDelay:1];
        
    }];
    
    [self.notificationsTableView.mj_header endRefreshing];
    
}

- (IBAction)historySelect:(id)sender {
    
    HistoryCalendarViewController * historyCalendarViewController = [[HistoryCalendarViewController alloc]init];
    
    [self showViewController:historyCalendarViewController sender:nil];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.array.count > 0) {
        
        self.noDataLabel.hidden = YES;
        
    }else{
        
        self.noDataLabel.hidden = NO;
        
    }
    
    return self.array.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dict = self.array[indexPath.row];
    
    NotificationsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"NotificationsCell"];
    
    if (cell == nil) {
        
        cell = [[NotificationsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NotificationsCell"];
        
    }
    
    NSString * date = [UserTools TimestampChangeTimestr:[dict valueForKey:@"EventTime"]];
    NSString * type = [dict valueForKey:@"AlarmType"];
    
    [cell setDate:date type:type serialNumber:[dict valueForKey:@"SerialNumber"]];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 104;
    
}



#pragma mark 获取列表(旧)
-(void)getNotifications{
    
    NSDictionary * dict = self.userModel.deviceByAccountArray[7];
    
    NSString * seriaNumber = [dict valueForKey:SERIAL_NUMBER];
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    
    parameters[LIST_SERIAL_NUMBER] = seriaNumber;
    parameters[USER_ID] = self.userModel.userID;
    parameters[CLIENT_TYPE] = self.userModel.clientType;
    parameters[TOKEN_PASS] = self.userModel.tokenPass;
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",SERVICE_URL,@"WebApi/GPSData/GetLastEventDataBySns"];
    
    [manager POST:postUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        
        NSLog(@"success %@",responseObject);
        
        self.array = responseObject;
        
        [self.notificationsTableView reloadData];
        
        [self.notificationsTableView.mj_header endRefreshing];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
        [self.notificationsTableView.mj_header endRefreshing];
        
    }];
    
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
